package org.example.test_zhanbao_chaibao;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.SocketChannel;

public class MyClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();

        System.out.println("客户端启动中...");

        try {
            //不带server 就是客户端
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                     .channel(NioSocketChannel.class)
                     .handler(new ChannelInitializer<SocketChannel>() {
                         @Override
                         protected void initChannel(SocketChannel ch) {
                             ChannelPipeline pipeline = ch.pipeline();

                             // 客户端无需拆包处理器
                             pipeline.addLast(new MyClientHandler());
                         }
                     });

            ChannelFuture future = bootstrap.connect("localhost", 7001).sync();
            System.out.println("客户端启动成功");
            future.channel().closeFuture().sync();

        } finally {
            group.shutdownGracefully();
        }
    }
}